package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.ProductInventory;
import com.example.mall.domain.ProductSpecs;
import com.example.mall.dto.ProductInventoryDto;
import com.example.mall.dto.ProductSpecsDto;
import com.example.mall.mapper.ProductSpecsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductInventoryQuery;
import com.example.mall.query.ProductSpecsQuery;
import com.example.mall.service.IProductSpecsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品规格表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class ProductSpecsServiceImpl extends ServiceImpl<ProductSpecsMapper, ProductSpecs> implements IProductSpecsService {

    @Autowired
    private ProductSpecsMapper productSpecsMapper;

    @Override
    public PageDTO<ProductSpecsDto> queryPage(ProductSpecsQuery productSpecsQuery) {
        if (productSpecsQuery == null) {
            productSpecsQuery = new ProductSpecsQuery();
        }
        Page<ProductSpecs> page = productSpecsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<ProductSpecs> cartPage = lambdaQuery()
                .like(productSpecsQuery.getSpecId()!= null, ProductSpecs::getSpecId, productSpecsQuery.getSpecId())
                .like(productSpecsQuery.getProductId()!= null, ProductSpecs::getProductId, productSpecsQuery.getProductId())
                .like(productSpecsQuery.getSpecName()!= null, ProductSpecs::getSpecName, productSpecsQuery.getSpecName())
                .like(productSpecsQuery.getSpecValue()!= null, ProductSpecs::getSpecValue, productSpecsQuery.getSpecValue())
                .orderByDesc(ProductSpecs::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, ProductSpecsDto.class);
    }

    @Override
    public ProductSpecs queryProductSpecsById(Long id) {
        AssertUtil.isNotNull(id,"标识不能为空");
        return productSpecsMapper.selectById(id);
    }

    @Override
    public List<ProductSpecs> queryProductSpecsList() {
        return this.list();
    }

    @Override
    public void insert(ProductSpecsDto productSpecsDto) {
        ProductSpecs productSpecs = new ProductSpecs();
        CopyUtils.copyProperties(productSpecsDto,productSpecs);
        productSpecs.setSpecId(CodeGenerateUtils.generateUniqueId());
        productSpecs.setCreateDate(new Date());
        productSpecs.setUpdateDate(new Date());
        productSpecsMapper.insert(productSpecs);
    }

    @Override
    public void update(ProductSpecsDto productSpecsDto) {
        AssertUtil.isNotNull(productSpecsDto.getSpecId(),"标识不能为空");
        ProductSpecs productSpecs = productSpecsMapper.selectById(productSpecsDto.getSpecId());
        AssertUtil.isNotNull(productSpecs,"记录不存在");
        CopyUtils.copyProperties(productSpecsDto,productSpecs);
        productSpecs.setUpdateDate(new Date());
        productSpecsMapper.updateById(productSpecs);
    }

    @Override
    public void delete(Long id) {
        AssertUtil.isNotNull(id,"标识不能为空");
        ProductSpecs productSpecs = productSpecsMapper.selectById(id);
        AssertUtil.isNotNull(productSpecs,"记录不存在");
        productSpecsMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        AssertUtil.isNotNull(ids,"标识不能为空");
        List<ProductSpecs> productSpecs = productSpecsMapper.selectBatchIds(ids);
        AssertUtil.isNotNull(productSpecs,"记录不存在");
        productSpecsMapper.deleteBatchIds(ids);
    }
}
